<template>
  <div class="directory-msg">
    <div class="top-panel__box">
      <el-form :model="dataForm" ref="dataForm" inline size="small" onsubmit="return false">
        <el-form-item>
          <el-input placeholder="字段名称" v-model="dataForm.dictName" maxlength="30" clearable />
        </el-form-item>
        <el-form-item>
          <el-input placeholder="代码" v-model="dataForm.dictNo" maxlength="30" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList(1)">
            <fa-icon class="fa-search" />查询</el-button>
          <reset-button :dataForm.sync="dataForm" :fixedData="{pageNum: 1, pageSize: 20}" />
          <el-button size="small" plain @click="dialog.importDialog = true">
            <fa-icon class="fa-upload" />导入</el-button>
          <el-button type="" size="small" plain @click="exportExcel">
            <fa-icon class="fa-download" />导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row class="table-top">
      <el-table :data="dataList" v-loading="tableLoading" tooltip-effect="light" class="tc-table" size="small">
        <el-table-column type="index" width="55" fixed="left" label="序号"></el-table-column>
        <template v-for="(column, index) in DICT_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
        <el-table-column prop="operation" class-name="oper-col" label="操作" :width="250">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="editOrFecthRow(scope.row, 'det')">详情</el-button>
            <el-button size="mini" type="primary" plain @click="editOrFecthRow(scope.row, 'edit')">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="editOrFecthRow(scope.row, 'delete')">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row class="pagination-top">
      <pagination :pageNum.sync="dataForm.pageNum" :curSizes.sync="dataForm.pageSize" :total="total" @getList="getList" />
    </el-row>
    <el-dialog :title="flag === 'det'?'字段值详情': '编辑字段值'" :visible.sync="dialog.detailDialog" width="30%" :close-on-click-modal="false">
      <dict-detatil v-if="dialog.detailDialog" :flag="flag" :dictId="dictId" @uppdataSuccess="uppdataSuccess('detail')" @close="closeDetails" />
    </el-dialog>
    <el-dialog :visible.sync="dialog.importDialog" width="80%" custom-class="import-dialog" :close-on-click-modal="false">
      <import-file v-if="dialog.importDialog" @updateData="uppdataSuccess('import')"></import-file>
    </el-dialog>
  </div>
</template>

<script>
import DictDetatil from './components/DictDetatil'
import ImportFile from './components/importFile'
import { DICT_HEAD_LIST } from './js/contant.js'
import { exportExcel } from '@/utils/util'
import { CONSTANT_OBJ } from '@/utils/constant'
import { common } from '@/utils/common'
import api from './api'
export default {
  components: { DictDetatil, ImportFile },
  data () {
    return {
      DICT_HEAD_LIST,
      dataList: [],
      tableLoading: false,
      flag: '',
      dialog: {
        detailDialog: false,
        importDialog: false
      },
      dictId: '',
      dataForm: {
        dictName: '',
        dictNo: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList (val) {
      this.dataForm.pageNum = val || this.dataForm.pageNum
      api.queryByDictList(this.dataForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataList = data.list || []
          this.total = parseInt(data.total) || 0
        }
      })
    },
    // 关闭弹框
    uppdataSuccess (dialogName) {
      this.getList(1)
      this.dialog[`${dialogName}Dialog`] = false
    },
    // 编辑|详情
    editOrFecthRow (row, flag) {
      if (flag !== 'delete') {
        this.dictId = row.id
        this.dialog.detailDialog = true
        this.flag = flag
      } else {
        common.confirmFn(`请确认是否删除该条字典项？`, '删除此字典项').then(res => {
          if (res) {
            console.log(row, '父项删除')
            api.deleteDict(row.id).then(res => {
              const { responseCode } = res
              if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
                this.getList(0)
                console.log('删除成功')
              }
            })
          }
        })
      }
    },
    closeDetails () {
      this.dialog.detailDialog = false
    },
    /**
     * 导出
     */
    exportExcel () {
      let parmas = {
        dictName: this.dataForm.dictName,
        dictNo: this.dataForm.dictNo
      }
      let path = `/v1/api/backend/sysDict/exportExcel`
      let name = '字典'
      exportExcel(path, parmas, name)
    }
  }
}
</script>

<style scoped>
.directory-msg /deep/ .import-dialog {
  height: 75vh;
}
</style>
